package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Outproduct;
import cn.xpg.service.OutproductService;
import cn.xpg.tools.PageSupport;

@Controller
public class OutproductController {
	@Resource(name = "outproductService")
	private OutproductService outproductService;

	// 分页查询出库单信息
	@RequestMapping("/getOutproductList.do")
	public String getOutproductList(HttpServletRequest request) {
		String product_name = request.getParameter("product_name");
		String pageIndex = request.getParameter("pageIndex");
		String sname=request.getParameter("sname");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = outproductService.getOutproductCount(product_name);
		System.out.println("查询出库单信息！！！！！！！！！！————>getOutproductList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Outproduct> outproductList = null;
		outproductList = outproductService.getOutproductList(product_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("outproductList", outproductList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("product_name", product_name);
		System.out.println(sname+"!!!!!!!!!!");
		if (sname.equals("list")) {
			return "/jsp/jinxiaocun/export_list";
		} else {
			return "/jsp/jinxiaocun/export_statistics";
		}
	}

	// 新增出库单
	@RequestMapping("addOutproduct.do")//没进这
	public String addOutproduct(@Valid Outproduct outproduct,HttpServletRequest requset) {
		System.out.println("新增出库单信息！！！！！！！！！！————>addOutproduct");
		if (outproductService.addOutproduct(outproduct) > 0) {
			return "redirect:/jsp/jinxiaocun/index";
		} else {
			return "/jsp/jinxiaocun/export_list";
		}
	}

	// 获取出库单Id
	@RequestMapping("getOutproductId.do")
	public String getOutproductId(HttpServletRequest request) {
		System.out.println("获取出库单Id！！！！！！！！！！————>getOutproductId");
		int outproduct_id = Integer.parseInt(request.getParameter("outproduct_id"));
		Outproduct outproduct = outproductService.getOutproductId(outproduct_id);
		request.setAttribute("outproduct", outproduct);
		return "/jsp/jinxiaocun/export_update";
	}

	// 修改出库单
	@RequestMapping("updateOutproduct.do")
	public String updateOutproduct(@Valid Outproduct outproduct, HttpServletRequest request) {
		System.out.println("修改出库单信息！！！！！！！！！！————>updateOutproduct");
		if (outproductService.updateOutproduct(outproduct) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getOutproductList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("outproduct", outproduct);
			return "/jsp/jinxiaocun/export_update";
		}
	}

	// 删除出库单
	@RequestMapping("deleteOutproduct.do")
	public String deleteOutproduct(HttpServletRequest request) {
		System.out.println("删除出库单信息！！！！！！！！！！————>deleteOutproduct");
		int outproduct_id = Integer.valueOf(request.getParameter("outproduct_id"));
		if (outproductService.deleteOutproduct(outproduct_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getOutproductList.do";
	}
}
